package com.ac.input.web.action.input;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
//import com.fulong.core.dm.User;
//import com.cbhb.core.dm.HndLog;
import com.caac.core.config.Config;
import com.caac.core.util.StringUtil;
import com.caac.core.util.FileUtils;
import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;
import com.ac.input.dao.BaseDao;
import com.ac.input.dao.ReportsDao;
import com.ac.input.web.form.DataForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class SaveDataAction extends BaseAction {
    protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws
            Exception {
        DataForm myForm = (DataForm) form;
//      User user = this.getUserFromCache(request, response);
//      String username = (user == null) ? "" : user.getId();
        
        if(myForm.getMultipartRequestHandler()!=null){
            FormFile[] formFile = null;
	        Hashtable files = myForm.getMultipartRequestHandler().getFileElements();
	        if (files != null && files.size() > 0){
	        	formFile = new FormFile[files.size()];
	        	Enumeration enums = files.keys();
	        	String fileKey = null;
	        	int i =0;
	        	while (enums.hasMoreElements()){
	        		fileKey = (String) (enums.nextElement());
	        		formFile[i] = (FormFile) files.get(fileKey);
	        		if(formFile[i].getFileSize()>0){
	        			String fileName = com.ac.input.util.UUID.randomUUID().toString()+StringUtil.popFileType(formFile[i].getFileName());
	        			FileUtils.upLoadFile(formFile[i], fileName,Config.getInstance().getRealPath()+"/resource/"+myForm.getMendId()+"/");
	        			myForm.setDynaProperty(fileKey, "/resource/"+myForm.getMendId()+"/"+fileName);
	        		}
	        		i++;
	        	}
	        }
        }
        
        ReportsDao rpsDao = (ReportsDao)this.getBean("reportsDao");
        if (!myForm.getMendId().equals(myForm.getOldMendId())
            || myForm.getRhid() == null) {
            myForm.setOldMendId(myForm.getMendId());
            myForm.setRhid(rpsDao.getHndInpDefById(myForm.getMendId()));
        }

        BaseDao baseDao = (BaseDao)this.getBean("hndBaseDao");
        if("2".equals(myForm.getRhid().getUptFlag())){
        	this.getJdbcTemplate().execute("{CALL PRC_BEF_SAVE('"+myForm.getMendId()+"','"+myForm.getKeyValue()+"')}");
        }
        
        baseDao.saveObject(myForm);
        
        if("2".equals(myForm.getRhid().getUptFlag())){
        	this.getJdbcTemplate().execute("{CALL PRC_AFT_SAVE('"+myForm.getMendId()+"','"+myForm.getKeyValue()+"')}");
        }
        
        if("1".equals(myForm.getIsDef())){
        	String acPath = "/pages/protected/"+myForm.getAcPath()+".jsp";
    		return new ActionForward(acPath);
        }

        return forward(mapping);
    }
}